package com.stuManager.util;

import com.stuManager.model.User;

import java.util.Random;

public class UserTokenUtil {
    private static Random rand;
    static {
        rand = new Random();
    }

    public static void main(String[] args) {
        createToken(null);
    }

    //生成用户所需的token，目前暂时只生成随机数
    public static String createToken(User user) {
        //生成大小写字母+10个数字的包含62个字符的字符串 26+26+10
        //长度设置为20个字符
        if (user == null) {
            return "";
        }
        if (user.getUserName().length() == 0) {
            return "";
        }
        String token = "";
        for (int i=0; i< 20; i++) {
            char character = 'a';
            int characterCode = rand.nextInt(62);
            if (characterCode < 26) {
                character = (char) (97 + characterCode);
            }
            else if (characterCode < 52) {
                character = (char) (65 + characterCode - 26);
            }
            else {
                character = (char)(48 + characterCode - 52);
            }
            token += character;
        }
        System.out.println("token is " + token);
        return token;
    }
}
